package com.bishe.controller;

import cn.hutool.core.util.StrUtil;
import com.bishe.pojo.Admin;
import com.bishe.pojo.LoginModel;
import com.bishe.pojo.Message;
import com.bishe.serivce.IAdminService;
import com.bishe.utils.EcoBootException;
import com.bishe.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * admin控制层
 * @date 2022/03/20 41:08:16
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "管理员控制器")
@CrossOrigin
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginModel loginModel){
        if (loginModel == null){
            throw new EcoBootException("用户名密码为空！");
        }
        if (StrUtil.isEmpty(loginModel.getUserName())||StrUtil.isEmpty(loginModel.getPassWord())){
            throw new EcoBootException("用户名或密码为空！");
        }

        LoginModel login = adminService.login(loginModel); //登录操作

        return  Result.ok("ok",login);
    }

    @ApiOperation("添加和修改Admin")
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Admin admin, HttpServletRequest httpServletRequest){
        if (admin == null){
            throw new EcoBootException("admin 为空！");
        }
        adminService.addOrUpdate(admin,httpServletRequest); //添加和修改

        return Result.ok();
    }

    @ApiOperation("删除admin")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id")String id){


        adminService.deleteById(id); //删除

        return Result.ok();
    }
}
